/*
 * @Author: your name
 * @Date: 2021-01-28 19:50:25
 * @LastEditTime: 2021-04-25 10:58:40
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vant\src\api\modules\secret.js
 */
import Vue from 'vue'
import CryptoJS from 'crypto-js'
import JSEncrypt from 'jsencrypt/bin/jsencrypt'
// AES-128-CBC偏移量
// const CBCIV = "abcdefgabcdefg12";
// 默认的 KEY 与 iv 如果没有给
const KEY = CryptoJS.enc.Utf8.parse("1234567890123456");
const IV = CryptoJS.enc.Utf8.parse('1234567890123456');

/**
 * 加密
 */
export default {
        encrypts(word, keyStr, ivStr) {
            let key = KEY;
            let iv = IV;  
            if (keyStr) {
              key = CryptoJS.enc.Utf8.parse(keyStr);
              iv = CryptoJS.enc.Utf8.parse(ivStr);
            }
            let srcs = CryptoJS.enc.Utf8.parse(word);
            var encrypted = CryptoJS.AES.encrypt(srcs, key, {
              iv: iv, 
              mode: CryptoJS.mode.CBC,
              padding: CryptoJS.pad.ZeroPadding
            });
            return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
          
        },
    
      /**
         * AES 解密 ：字符串 key iv  返回base64
         *
         * @return {string}
         */
    decrypts(word, keyStr, ivStr) {
    let key  = KEY;
    let iv = IV;
  
    if (keyStr) {
      key = CryptoJS.enc.Utf8.parse(keyStr);
      iv = CryptoJS.enc.Utf8.parse(ivStr);
    }
  
    let base64 = CryptoJS.enc.Base64.parse(word);
    let src = CryptoJS.enc.Base64.stringify(base64);
  
    let decrypt = CryptoJS.AES.decrypt(src, key, {
      iv: iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.ZeroPadding
    });
  
    let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
    return decryptedStr.toString();
  }
}